package com.zhuifeng.smart.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

import com.zhuifeng.smart.model.Webs;
import com.zhuifeng.smart.services.WebsServices;
import com.zhuifeng.smart.services.impl.WebsServicesImpl;

/**
 * Servlet implementation class WebsAjax
 */
@WebServlet("/webs_ajax.do")
public class WebsAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public WebsAjax() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");

		String type = request.getParameter("type");
		if ("add".equals(type)) {
			this.addWews(request, response);
			return;
		} else if ("edit".equals(type)) {
			this.editWebs(request, response);
			return;
		} else if ("delete".equals(type)) {
			this.deleteWebs(request, response);
			return;
		}
		this.getWews(request, response);
	}

	// 获取网址
	private void getWews(HttpServletRequest request,
			HttpServletResponse response) {
		String websort = request.getParameter("websort");
		WebsServices websServices = new WebsServicesImpl();
		List<Webs> websList = websServices.getWebsList(websort);
		// for (Webs s : websList) {
		// System.out.println(s);
		// }
		String json = null;
		Object obj = JSONSerializer.toJSON(websList);
		json = obj.toString();
		json = "{data:" + json + "}";
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(json);
		out.close();
	}

	// 添加网址
	private void addWews(HttpServletRequest request,
			HttpServletResponse response) {
		WebsServices websServices = new WebsServicesImpl();
		String webname = request.getParameter("webname");
		String website = request.getParameter("website");
		String websort = request.getParameter("websort");
		int isAddWebs = websServices.addNews(new Webs(0, webname, website,
				websort, "0", ""));
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(isAddWebs);
		out.close();
	}

	// 编辑网址
	private void editWebs(HttpServletRequest request,
			HttpServletResponse response) {
		WebsServices websServices = new WebsServicesImpl();
		String webid = request.getParameter("webid");
		String webname = request.getParameter("webname");
		String website = request.getParameter("website");
		String websort = request.getParameter("websort");

		boolean isUpdataWebs = websServices.updataNews(new Webs(Integer
				.parseInt(webid), webname, website, websort, "0", ""));
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(isUpdataWebs);
		out.close();
	}

	// 删除网址
	private void deleteWebs(HttpServletRequest request,
			HttpServletResponse response) {
		WebsServices websServices = new WebsServicesImpl();
		String webid = request.getParameter("webid");

		boolean isDeleteNews = websServices.deleteNews(webid);
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(isDeleteNews);
		out.close();
	}
}
